﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

//带有挤压动画的Layout
public class DragLayoutGroupPro : DragLayoutGroupBase
{
    public bool lockX;
    public bool lockY;
    private void Start()
    {
        for (int i = 0; i < childGroupRectTransform.childCount; i++)
        {
            DragLayoutPro dragLayout = childGroupRectTransform.GetChild(i).gameObject.AddComponent<DragLayoutPro>();
            dragLayout.restrictedRect = childGroupRectTransform;
            dragLayout.lockX = lockX;
            dragLayout.lockY = lockY;
            dragLayout.dragLayoutGroup = this;
        }
    }

    /// <summary>
    /// 改不会，效果就先这样吧，有一定的动态效果了。
    /// </summary>
    /// <param name="drag"></param>
    public void SqueezeAsideStart(DragLayoutPro drag)
    {
        for (int i = 0; i < childGroupRectTransform.childCount; i++)
        {
            var child = childGroupRectTransform.GetChild(i).GetComponent<DragLayoutPro>();
            if (drag.transform.position.y > childGroupRectTransform.GetChild(i).position.y)
            {
                child.BaseLocalPosition = child.rectTransform.localPosition;
            }
            else
            {
                Vector3 offset = new Vector3(0, drag.rectTransform.sizeDelta.y, 0);
                child.BaseLocalPosition = child.rectTransform.localPosition - offset;
                //child.rectTransform.localPosition = child.BaseLocalPosition;
                Debug.Log("开始" + child.rectTransform.localPosition);
            }
        }
    }


    /// <param name = "drag" ></ param >
    /// < param name="Other"></param>
    public void SqueezeAside(DragLayoutPro drag)
    {

        if (childGroup is VerticalLayoutGroup)
        {
            //Vector3 offset = 0;
            //posIndex = (int)target.anchoredPosition.y;
            for (int i = 0; i < childGroupRectTransform.childCount; i++)
            {
                var child = childGroupRectTransform.GetChild(i).GetComponent<DragLayoutPro>();
                if (drag.transform.position.y < childGroupRectTransform.GetChild(i).position.y)
                {

                    Vector2 offset = drag.rectTransform.sizeDelta;
                    offset.x = 0;
                    //offset.y = 0;
                    Vector2 lerp = child.BaseLocalPosition + offset;
                    child.rectTransform.localPosition = Vector2.Lerp(child.rectTransform.localPosition, lerp, 0.5f);
                    Debug.Log("额外" + child.rectTransform.localPosition);
                    //offset += rectTransform.rect.height;//长度
                }
                else
                {
                    Vector3 lerp = child.BaseLocalPosition;
                    child.rectTransform.localPosition = Vector2.Lerp(child.rectTransform.localPosition, lerp, 0.5f);
                }
            }
        }
    }
}

